//
//  BaseViewController.m
//  BMHeadLine
//
//  Created by 徐益智 on 16/1/20.
//  Copyright © 2016年 Vdianjing. All rights reserved.
//

#import "BaseViewController.h"

#import "Header.h"
#import "MarkHeader.h"

@interface BaseViewController ()
<UIGestureRecognizerDelegate>


@end

@implementation BaseViewController


#pragma mark - view life

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.navigationController.navigationBarHidden = YES;
    
    [self initSubViews];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (IOS7orLater) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    
    // 关闭
    if (self.drag == kCanNotDrag) {
        MyLog(@"=======can not drag");
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if (self.drag == kCanNotDrag) {
        MyLog(@"--------can drag");
        // 开启
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}

- (void)initSubViews
{
    [self.view addSubview:self.navBarView];
    if (self.navigationController.viewControllers.count > 1) {
        [self.navBarView addSubview:self.leftButton];
    }
    [self.navBarView addSubview:self.titleLabel];
}

#pragma mark - event response
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)leftButtonItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (float)getStartOriginY
{
    if (IOS7orLater) {
        return 64;
    }else {
        return 44;
    }
}

- (float)getContentViewHeight
{
    return kScreenHeight - [self getStartOriginY];
}

#pragma mark - setter / getter
- (UIView *)noDataView
{
    if (!_noDataView) {
        CGFloat viewHeight = 180;
        CGFloat viewWidth = kScreenWidth;
        _noDataView = [[UIView alloc] init];
        _noDataView.sd_layout
        .centerYEqualToView(self.view)
        .centerXEqualToView(self.view)
        .heightIs(viewHeight)
        .widthIs(viewWidth);
        
        UIImageView *imageV = [[UIImageView alloc] initWithImage:kNodataImage];
        imageV.center = CGPointMake(_noDataView.frame.size.width / 2.0, _noDataView.frame.size.height / 2.0 - 20);
        [_noDataView addSubview:imageV];
        
        UILabel *titleLabel = [UILabel labelWithFont:14.f withTextColor:kGrayFontColor withBackgroundColor:[UIColor clearColor] withTextAlignment:NSTextAlignmentCenter withText:@"这里没有数据哦" withNumberLines:1];
        titleLabel.frame = CGRectMake(0, kGetY(imageV.frame) + 5, kScreenWidth, 40);
        [_noDataView addSubview:titleLabel];
    }
    return _noDataView;
}

- (UIButton *)leftButton
{
    if (!_leftButton) {
        
        CGFloat buttonHeight = 18;
        CGFloat oriY = (44 - buttonHeight) / 2.0 + 20;
        
        _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(20, oriY, 19, buttonHeight)];
        [_leftButton setImage:[UIImage imageNamed:@"back_arrow"] forState:UIControlStateNormal];
        [_leftButton addTarget:self action:@selector(leftButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIView *)navBarView
{
    if (!_navBarView) {
        _navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
        _navBarView.backgroundColor = kNavBackgroundColor;
    }
    return _navBarView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel ) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kGetX(_leftButton.frame), 20, kScreenWidth - kGetX(_leftButton.frame)*2, 44)];
        _titleLabel.backgroundColor = kClearClolor;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = kNavFontColor;
        _titleLabel.font = [UIFont systemFontOfSize:17];
    }
    return _titleLabel;
}

@end
